package cn.kgc.web;

import cn.kgc.entity.Teacher;
import cn.kgc.service.TeacherService;
import cn.kgc.service.impl.TeacherServiceImpl;
import cn.kgc.util.Pager;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@WebServlet(urlPatterns = "/teacher")
public class TeacherServlet extends BaseServlet {
    //查询
    public String search(HttpServletRequest request, HttpServletResponse response){
        int currentPage = 1;
        int currentSize = 8;
        //获取页码
        String currentPagestr = request.getParameter("cp");
        //获取每页显示的条数
        String currentSizestr = request.getParameter("ps");
        if(currentPagestr!=null && !"".equals(currentPagestr)){
            currentPage = Integer.parseInt(currentPagestr);
        }
        if(currentSizestr!=null && !"".equals(currentSizestr)){
            currentSize = Integer.parseInt(currentSizestr);
        }
        //获取工号，姓名，专业 进行模糊查询
        String jobnum = request.getParameter("jobnum");
        String name = request.getParameter("name");
        String specialty =request.getParameter("specialty");
        //执行查询方法
        TeacherService teacherService = new TeacherServiceImpl();
        List<Teacher> teacherList = teacherService.search(currentPage,currentSize,jobnum,name,specialty);

        //获取总页数，设置起始页码和结束页码
        int totalPage = teacherService.totalPage(currentSize,jobnum,name,specialty);
        int begin = currentPage-2;
        int end = currentPage+2;
        if(currentPage<=2){
            begin = 1;
        }
        if(currentPage>=totalPage-2){
            end = totalPage;
        }

        //获取总的记录数
        int totalCount = teacherService.totalCount(jobnum,name,specialty);
        //将总的记录数，当前页码，每页显示条数，页码数量放入pager对象中
        Pager pager = new Pager(totalCount,currentPage,currentSize,10);
        //将查询结果集teacherList放入Pager对象中
        pager.setList(teacherList);
        HttpSession session = request.getSession();
        session.setAttribute("begin",begin);
        session.setAttribute("end",end);
        session.setAttribute("pager",pager);
        session.setAttribute("currentPage",currentPage);
        session.setAttribute("currentSize",currentSize);
        session.setAttribute("totalPage",totalPage);
        session.setAttribute("jobnum",jobnum);
        session.setAttribute("name",name);
        session.setAttribute("specialty",specialty);

        return request.getContextPath()+"/adm-teainfo.jsp";
    }
    //增加：
    public String add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 从请求参数中获取教师的各个属性值
        String jobNum = request.getParameter("jobNum");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        int age = 20;
        String agestr = request.getParameter("age");
        if(agestr!=null&&!"".equals(agestr)){
            age = Integer.parseInt(agestr);
        }
        String nation = request.getParameter("nation");
        String birthdatestr = request.getParameter("birthdate");
        String specialty = request.getParameter("specialty");
        String politicsStatus = request.getParameter("politicsStatus");
        String jobTitle = request.getParameter("jobTitle");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");


        //类型转换：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdate = null;
        if(birthdatestr!=null&&!"".equals(birthdatestr)){
            try {
                birthdate = simpleDateFormat.parse(birthdatestr);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        // 创建Teacher对象并设置属性值
        Teacher teacher = new Teacher();
        teacher.setJobnum(jobNum);
        teacher.setName(name);
        teacher.setGender(gender);
        teacher.setAge(age);
        teacher.setNation(nation);
//        if(birthdate!=null&&!"".equals(birthdate)){
            teacher.setBirthdate(birthdate);
//        }
        teacher.setSpecialty(specialty);
        teacher.setPoliticsstatus(politicsStatus);
        teacher.setJobtitle(jobTitle);
        teacher.setAddress(address);
        teacher.setPhone(phone);
        // 创建TeacherDao对象
        TeacherService teacherService = new TeacherServiceImpl();

        int flag = teacherService.add(teacher);
        if (flag > 0) {
            return request.getContextPath()+"/teacher?methodName=search";// 插入成功，重定向到教师列表页面
        } else {
            return request.getContextPath()+"/adm-teainfo.jsp";
            //错误页面
        }

    }

    //根据id查找信息 回显
    public void findById(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String ids = request.getParameter("id");
        PrintWriter out = response.getWriter();
        TeacherService teacherService = new TeacherServiceImpl();
        Teacher teacher = teacherService.findById(ids);
        String json = JSON.toJSONString(teacher);
        out.print(json);
    }

    public void findById2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        String ids = request.getParameter("id");
        PrintWriter out = response.getWriter();
        TeacherService teacherService = new TeacherServiceImpl();
        Teacher teacher = teacherService.findById(ids);
        out.print(teacher.getBirthdate());
    }

    //修改选中教师信息
    public String modifyTeacher(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //取值
        String ids = request.getParameter("id");
        String jobNum = request.getParameter("jobNum");
        String name = request.getParameter("name");
        String gender = request.getParameter("gender");
        String age = request.getParameter("age");
        String nation = request.getParameter("nation");
        String birthdatestr = request.getParameter("birthdate");
        String specialty = request.getParameter("specialty");
        String politicsstatus = request.getParameter("politicsStatus");
        String jobtitle = request.getParameter("jobTitle");
        String address = request.getParameter("address");
        String phone = request.getParameter("phone");
        //类型转换：
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdate = null;
        try {
            birthdate = simpleDateFormat.parse(birthdatestr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        TeacherService teacherService = new TeacherServiceImpl();
        //传值太多 创建一个教师类
        Teacher teacher = new Teacher();
        teacher.setId(Long.parseLong(ids));
        teacher.setJobnum(jobNum);
        teacher.setName(name);
        teacher.setGender(gender);
        teacher.setAge(Long.parseLong(age));
        teacher.setNation(nation);
        teacher.setBirthdate(birthdate);
        teacher.setSpecialty(specialty);
        teacher.setPoliticsstatus(politicsstatus);
        teacher.setJobtitle(jobtitle);
        teacher.setAddress(address);
        teacher.setPhone(phone);
        teacherService.modifyTeacher(teacher);

        return request.getContextPath()+"/teacher?methodName=search";
    }
}
